﻿#pragma once
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include "IPC.h"

ImageProcessing ipFront;
ImageProcessing ipSide;
char* inFilenameFront = NULL;
char* inFilenameSide= NULL;
char** outFilenameFront= NULL;
char** outFilenameSide = NULL;
char* error = NULL;
namespace GUI_BodyFeaturePoints {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Runtime::InteropServices;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			inFilenameFront = new char[100];
			inFilenameSide = new char[100];
			error = new char[300];
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			inFilenameFront = NULL;
			inFilenameSide = NULL;
			error = NULL;
			delete[] inFilenameFront;
			delete[] inFilenameSide;
			delete[] error;
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::PictureBox^  imgOriginF;
	protected: 
	private: System::Windows::Forms::PictureBox^  imgOriginS;










	private: System::Windows::Forms::OpenFileDialog^  openFileFront;
	private: System::Windows::Forms::OpenFileDialog^  openFileSide;

	private: System::Windows::Forms::GroupBox^  groupBoxInput;
	private: System::Windows::Forms::Button^  btnOpenSide;
	private: System::Windows::Forms::Button^  btnOpenFront;
	private: System::Windows::Forms::Button^  btnRun;
	private: System::Windows::Forms::GroupBox^  groupBox1;
	private: System::Windows::Forms::TextBox^  txtSodo;
	private: System::Windows::Forms::Label^  lblFileSodo;
	private: System::Windows::Forms::RadioButton^  rbtDiemMoc;

	private: System::Windows::Forms::RadioButton^  rbtKetqua;
	private: System::Windows::Forms::RadioButton^  rbtBienMaHoa;
	private: System::Windows::Forms::RadioButton^  rtbAnhGoc;
	private: System::Windows::Forms::RadioButton^  rbtBong;
	private: System::Windows::Forms::RadioButton^  rbtDuongBien;










	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->imgOriginF = (gcnew System::Windows::Forms::PictureBox());
			this->imgOriginS = (gcnew System::Windows::Forms::PictureBox());
			this->openFileFront = (gcnew System::Windows::Forms::OpenFileDialog());
			this->openFileSide = (gcnew System::Windows::Forms::OpenFileDialog());
			this->groupBoxInput = (gcnew System::Windows::Forms::GroupBox());
			this->rtbAnhGoc = (gcnew System::Windows::Forms::RadioButton());
			this->rbtBong = (gcnew System::Windows::Forms::RadioButton());
			this->rbtDuongBien = (gcnew System::Windows::Forms::RadioButton());
			this->rbtBienMaHoa = (gcnew System::Windows::Forms::RadioButton());
			this->rbtDiemMoc = (gcnew System::Windows::Forms::RadioButton());
			this->rbtKetqua = (gcnew System::Windows::Forms::RadioButton());
			this->btnOpenFront = (gcnew System::Windows::Forms::Button());
			this->btnRun = (gcnew System::Windows::Forms::Button());
			this->btnOpenSide = (gcnew System::Windows::Forms::Button());
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->lblFileSodo = (gcnew System::Windows::Forms::Label());
			this->txtSodo = (gcnew System::Windows::Forms::TextBox());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->imgOriginF))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->imgOriginS))->BeginInit();
			this->groupBoxInput->SuspendLayout();
			this->groupBox1->SuspendLayout();
			this->SuspendLayout();
			// 
			// imgOriginF
			// 
			this->imgOriginF->BackColor = System::Drawing::Color::White;
			this->imgOriginF->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
			this->imgOriginF->Location = System::Drawing::Point(11, 52);
			this->imgOriginF->Name = L"imgOriginF";
			this->imgOriginF->Size = System::Drawing::Size(377, 499);
			this->imgOriginF->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
			this->imgOriginF->TabIndex = 0;
			this->imgOriginF->TabStop = false;
			// 
			// imgOriginS
			// 
			this->imgOriginS->BackColor = System::Drawing::Color::White;
			this->imgOriginS->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Center;
			this->imgOriginS->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
			this->imgOriginS->Location = System::Drawing::Point(394, 52);
			this->imgOriginS->Name = L"imgOriginS";
			this->imgOriginS->Size = System::Drawing::Size(168, 499);
			this->imgOriginS->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
			this->imgOriginS->TabIndex = 1;
			this->imgOriginS->TabStop = false;
			// 
			// groupBoxInput
			// 
			this->groupBoxInput->Controls->Add(this->rtbAnhGoc);
			this->groupBoxInput->Controls->Add(this->rbtBong);
			this->groupBoxInput->Controls->Add(this->rbtDuongBien);
			this->groupBoxInput->Controls->Add(this->rbtBienMaHoa);
			this->groupBoxInput->Controls->Add(this->rbtDiemMoc);
			this->groupBoxInput->Controls->Add(this->rbtKetqua);
			this->groupBoxInput->Controls->Add(this->imgOriginF);
			this->groupBoxInput->Controls->Add(this->btnOpenFront);
			this->groupBoxInput->Controls->Add(this->btnRun);
			this->groupBoxInput->Controls->Add(this->btnOpenSide);
			this->groupBoxInput->Controls->Add(this->imgOriginS);
			this->groupBoxInput->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10));
			this->groupBoxInput->Location = System::Drawing::Point(6, 10);
			this->groupBoxInput->Name = L"groupBoxInput";
			this->groupBoxInput->Size = System::Drawing::Size(568, 701);
			this->groupBoxInput->TabIndex = 11;
			this->groupBoxInput->TabStop = false;
			this->groupBoxInput->Text = L"QUẢN LÝ ẢNH";
			// 
			// rtbAnhGoc
			// 
			this->rtbAnhGoc->AutoSize = true;
			this->rtbAnhGoc->Location = System::Drawing::Point(45, 647);
			this->rtbAnhGoc->Name = L"rtbAnhGoc";
			this->rtbAnhGoc->Size = System::Drawing::Size(109, 21);
			this->rtbAnhGoc->TabIndex = 10;
			this->rtbAnhGoc->TabStop = true;
			this->rtbAnhGoc->Text = L"Xem anh goc";
			this->rtbAnhGoc->UseVisualStyleBackColor = true;
			this->rtbAnhGoc->CheckedChanged += gcnew System::EventHandler(this, &Form1::rtbAnhGoc_CheckedChanged);
			// 
			// rbtBong
			// 
			this->rbtBong->AutoSize = true;
			this->rbtBong->Location = System::Drawing::Point(195, 647);
			this->rbtBong->Name = L"rbtBong";
			this->rbtBong->Size = System::Drawing::Size(121, 21);
			this->rbtBong->TabIndex = 9;
			this->rbtBong->TabStop = true;
			this->rbtBong->Text = L"Xem hinh bong";
			this->rbtBong->UseVisualStyleBackColor = true;
			this->rbtBong->CheckedChanged += gcnew System::EventHandler(this, &Form1::rbtBong_CheckedChanged);
			// 
			// rbtDuongBien
			// 
			this->rbtDuongBien->AutoSize = true;
			this->rbtDuongBien->Location = System::Drawing::Point(345, 647);
			this->rbtDuongBien->Name = L"rbtDuongBien";
			this->rbtDuongBien->Size = System::Drawing::Size(129, 21);
			this->rbtDuongBien->TabIndex = 8;
			this->rbtDuongBien->TabStop = true;
			this->rbtDuongBien->Text = L"Xem duong bien";
			this->rbtDuongBien->UseVisualStyleBackColor = true;
			this->rbtDuongBien->CheckedChanged += gcnew System::EventHandler(this, &Form1::rbtDuongBien_CheckedChanged);
			// 
			// rbtBienMaHoa
			// 
			this->rbtBienMaHoa->AutoSize = true;
			this->rbtBienMaHoa->Location = System::Drawing::Point(345, 606);
			this->rbtBienMaHoa->Name = L"rbtBienMaHoa";
			this->rbtBienMaHoa->Size = System::Drawing::Size(180, 21);
			this->rbtBienMaHoa->TabIndex = 7;
			this->rbtBienMaHoa->TabStop = true;
			this->rbtBienMaHoa->Text = L"Xem duong bien ma hoa";
			this->rbtBienMaHoa->UseVisualStyleBackColor = true;
			this->rbtBienMaHoa->CheckedChanged += gcnew System::EventHandler(this, &Form1::rbtBienMaHoa_CheckedChanged);
			// 
			// rbtDiemMoc
			// 
			this->rbtDiemMoc->AutoSize = true;
			this->rbtDiemMoc->Location = System::Drawing::Point(195, 606);
			this->rbtDiemMoc->Name = L"rbtDiemMoc";
			this->rbtDiemMoc->Size = System::Drawing::Size(118, 21);
			this->rbtDiemMoc->TabIndex = 6;
			this->rbtDiemMoc->TabStop = true;
			this->rbtDiemMoc->Text = L"Xem diem moc";
			this->rbtDiemMoc->UseVisualStyleBackColor = true;
			this->rbtDiemMoc->CheckedChanged += gcnew System::EventHandler(this, &Form1::rbtDiemMoc_CheckedChanged);
			// 
			// rbtKetqua
			// 
			this->rbtKetqua->AutoSize = true;
			this->rbtKetqua->Location = System::Drawing::Point(45, 606);
			this->rbtKetqua->Name = L"rbtKetqua";
			this->rbtKetqua->Size = System::Drawing::Size(105, 21);
			this->rbtKetqua->TabIndex = 5;
			this->rbtKetqua->TabStop = true;
			this->rbtKetqua->Text = L"Xem ket qua";
			this->rbtKetqua->UseVisualStyleBackColor = true;
			this->rbtKetqua->CheckedChanged += gcnew System::EventHandler(this, &Form1::rbtKetqua_CheckedChanged);
			// 
			// btnOpenFront
			// 
			this->btnOpenFront->Location = System::Drawing::Point(11, 20);
			this->btnOpenFront->Name = L"btnOpenFront";
			this->btnOpenFront->Size = System::Drawing::Size(377, 26);
			this->btnOpenFront->TabIndex = 2;
			this->btnOpenFront->Text = L"Mở ảnh trước ...";
			this->btnOpenFront->UseVisualStyleBackColor = true;
			this->btnOpenFront->Click += gcnew System::EventHandler(this, &Form1::btnOpenFront_Click);
			// 
			// btnRun
			// 
			this->btnRun->Location = System::Drawing::Point(11, 557);
			this->btnRun->Name = L"btnRun";
			this->btnRun->Size = System::Drawing::Size(551, 27);
			this->btnRun->TabIndex = 4;
			this->btnRun->Text = L"Chạy thuật toán";
			this->btnRun->UseVisualStyleBackColor = true;
			this->btnRun->Click += gcnew System::EventHandler(this, &Form1::btnRun_Click);
			// 
			// btnOpenSide
			// 
			this->btnOpenSide->Location = System::Drawing::Point(394, 20);
			this->btnOpenSide->Name = L"btnOpenSide";
			this->btnOpenSide->Size = System::Drawing::Size(168, 26);
			this->btnOpenSide->TabIndex = 3;
			this->btnOpenSide->Text = L"Mở ảnh hông ...";
			this->btnOpenSide->UseVisualStyleBackColor = true;
			this->btnOpenSide->Click += gcnew System::EventHandler(this, &Form1::btnOpenSide_Click);
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->lblFileSodo);
			this->groupBox1->Controls->Add(this->txtSodo);
			this->groupBox1->Location = System::Drawing::Point(580, 10);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(415, 701);
			this->groupBox1->TabIndex = 12;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = L"KẾT QUẢ";
			// 
			// lblFileSodo
			// 
			this->lblFileSodo->AllowDrop = true;
			this->lblFileSodo->Location = System::Drawing::Point(10, 557);
			this->lblFileSodo->Name = L"lblFileSodo";
			this->lblFileSodo->Size = System::Drawing::Size(394, 51);
			this->lblFileSodo->TabIndex = 1;
			this->lblFileSodo->Text = L"File So do: ";
			// 
			// txtSodo
			// 
			this->txtSodo->AllowDrop = true;
			this->txtSodo->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10));
			this->txtSodo->Location = System::Drawing::Point(13, 20);
			this->txtSodo->Multiline = true;
			this->txtSodo->Name = L"txtSodo";
			this->txtSodo->Size = System::Drawing::Size(396, 529);
			this->txtSodo->TabIndex = 0;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->AutoScroll = true;
			this->BackColor = System::Drawing::SystemColors::Control;
			this->ClientSize = System::Drawing::Size(996, 723);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->groupBoxInput);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
			this->Name = L"Form1";
			this->Text = L"Xác định điểm mốc cơ thể";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->Leave += gcnew System::EventHandler(this, &Form1::Form1_Leave);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->imgOriginF))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->imgOriginS))->EndInit();
			this->groupBoxInput->ResumeLayout(false);
			this->groupBoxInput->PerformLayout();
			this->groupBox1->ResumeLayout(false);
			this->groupBox1->PerformLayout();
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
			 }
private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
		 }

private: System::Void Process(System::Object^  sender, System::EventArgs^  e) {

			IplImage *img;
			char* outFilename = new char[100];
			//if (ipFront.GetImageObj())
			//{
			//	ipFront.RGBtoHSV();
			//	ipFront.FilterOnHSV(150,280,0,255,0,0);
			//	ipFront.HSVtoRGB();
			//	//extract silhouette
			//	img = ipFront.GetImageObj();
			//	outFilename = "images\\SilhouetteFront_out.png";
			//	cvSaveImage(outFilename,img);
			//	imgSilhouetteF->ImageLocation = Marshal::PtrToStringAnsi((IntPtr)(void*)outFilename);
			//	//extract edge using canny method
			//	ipFront.SetupGrayImage();
			//	ipFront.MakeCannyEdge();	
			//	img = ipFront.GetImageGrayObj();
			//	outFilename = "images\\CannyEdgeFront_out.png";
			//	imgChainBoundF->ImageLocation = Marshal::PtrToStringAnsi((IntPtr)(void*)outFilename);
			//	cvSaveImage(outFilename,img);
			//	//Create chain code
			//	if(!ipFront.CreateChainCode())
			//	{
			//		error = ipFront.ErrorReport(ipFront.nErrorID);
			//		MessageBox::Show(Marshal::PtrToStringAnsi((IntPtr)(void*)error),"ERROR", MessageBoxButtons::OK);	
			//	}
			//	img = ipFront.GetImageGrayObj();
			//	outFilename = "images\\ChainBoundFront_out.png";
			//	imgFeatureF->ImageLocation = Marshal::PtrToStringAnsi((IntPtr)(void*)outFilename);
			//	cvSaveImage(outFilename,img);
			//}
		 //
		 //if (ipSide.GetImageObj())
			//{
			//	ipSide.RGBtoHSV();
			//	ipSide.FilterOnHSV(150,280,0,255,0,0);
			//	ipSide.HSVtoRGB();
			//	//extract silhouette
			//	img = ipSide.GetImageObj();
			//	outFilename = "images\\SilhouetteSide_out.png";
			//	cvSaveImage(outFilename,img);
			//	imgSilhouetteS->ImageLocation = Marshal::PtrToStringAnsi((IntPtr)(void*)outFilename);
			//	//extract edge using canny method
			//	ipSide.SetupGrayImage();
			//	ipSide.MakeCannyEdge();	
			//	img = ipSide.GetImageGrayObj();
			//	outFilename = "images\\CannyEdgeSide_out.png";
			//	imgChainBoundS->ImageLocation = Marshal::PtrToStringAnsi((IntPtr)(void*)outFilename);
			//	cvSaveImage(outFilename,img);
			//	//Create chain code
			//	if(!ipSide.CreateChainCode())
			//	{
			//		error = ipFront.ErrorReport(ipFront.nErrorID);
			//		MessageBox::Show(Marshal::PtrToStringAnsi((IntPtr)(void*)error),"ERROR", MessageBoxButtons::OK);	
			//	};
			//	img = ipSide.GetImageGrayObj();
			//	outFilename = "images\\ChainBoundSide_out.png";
			//	imgFeatureS->ImageLocation = Marshal::PtrToStringAnsi((IntPtr)(void*)outFilename);
			//	cvSaveImage(outFilename,img);
			//}
	}
private: System::Void btnOpenFront_Click(System::Object^  sender, System::EventArgs^  e) {
			 openFileFront->InitialDirectory = L"Images\\";
			 openFileFront->Filter = L"Imgage files (*.png; *.jpg)|*.png; *.jpg|All files (*.*)|*.*";
			 openFileFront->Multiselect = false;
			 System::IO::Stream^ fileStream;

			 if ( openFileFront->ShowDialog() == System::Windows::Forms::DialogResult::OK )
			 {	
				 if ( (fileStream = openFileFront->OpenFile()) != nullptr )
				 {
					 //Cursor = System::Windows::Forms::Cursors::WaitCursor ;
					 imgOriginF->ImageLocation= openFileFront->FileName;

					 inFilenameFront =  (char*)(void*)Marshal::StringToHGlobalAnsi(openFileFront->FileName);
					
					 if(!ipFront.LoadImage(inFilenameFront))
					 {
						 error = ipFront.ErrorReport(ipFront.nErrorID);
						 MessageBox::Show(Marshal::PtrToStringAnsi((IntPtr)(void*)error),"ERROR", MessageBoxButtons::OK);				
					 };
					 fileStream->Close();
				 }
			 }
		 }
private: System::Void btnOpenSide_Click(System::Object^  sender, System::EventArgs^  e) {
			 openFileSide->InitialDirectory = L"Images\\";
			 openFileSide->Filter = L"Imgage files (*.png; *.jpg)|*.png; *.jpg|All files (*.*)|*.*";
			 openFileSide->Multiselect = false;

			 System::IO::Stream^ fileStream;

			 if ( openFileSide->ShowDialog() == System::Windows::Forms::DialogResult::OK )
			 {	
				 if ( (fileStream = openFileSide->OpenFile()) != nullptr )
				 {
					 //Cursor = System::Windows::Forms::Cursors::WaitCursor ;
					 imgOriginS->ImageLocation= openFileSide->FileName;

					 inFilenameSide =  (char*)(void*)Marshal::StringToHGlobalAnsi(openFileSide->FileName);
					
					 if(!ipSide.LoadImage(inFilenameSide))
					 {
						 error = ipSide.ErrorReport(ipSide.nErrorID);
						 MessageBox::Show(Marshal::PtrToStringAnsi((IntPtr)(void*)error),"ERROR", MessageBoxButtons::OK);	
					 };
					 fileStream->Close();
				 }
			 }
		 }
bool FindFeaturePoint(ImageProcessing ip,int type,Feature &f)
{
	for (int i=1;i<ip.nCountFeaturePoints;i++)
	{
		if(ip.features[i].type==type)
		{
			f=ip.features[i];
			return true;
		}
	}
	return false;
}
int ConvertToSideHeight(ImageProcessing ipF, ImageProcessing ipS, int lF)
{
	Feature fG;
	if(!FindFeaturePoint(ipS,SideGotChan,fG))
		return 0;

	int nHeightOfSide =fG.y - ipS.Ymin+1;
	int nHeightOfFront = ipF.Ymax - ipF.Ymin-20+1;
	int nHeightInSide = int( ((float)lF/(float)nHeightOfFront)*(float)nHeightOfSide);

	return nHeightInSide;
}
private: System::Void btnRun_Click(System::Object^  sender, System::EventArgs^  e) {
			 Cursor = System::Windows::Forms::Cursors::WaitCursor ;
			 IplImage* img = NULL;
			 outFilenameFront = new char*[6];
			 outFilenameSide = new char*[6];
			 for(int i=0;i<6;i++)
			 {
				 outFilenameFront[i] = new char[100];
				 outFilenameSide[i] = new char[100];
			 }
			 char* outfileSodo = new char[100];

			 ipFront.ExportFeatures(img,0,inFilenameFront,outFilenameFront);
			 img = NULL;
			 ipSide.ExportFeatures(img,1,inFilenameSide,outFilenameSide);

			 ipFront.GetFeaturesFromSide(ipSide);
			 ipFront.DrawFeaturePoints();
			 cvSaveImage(outFilenameFront[5],ipFront.GetImageOri());

			 ipSide.GetFeaturesFromFront(ipFront);
			 ipSide.DrawFeaturePoints();
			 cvSaveImage(outFilenameSide[5],ipSide.GetImageOri());

			 imgOriginF->ImageLocation = Marshal::PtrToStringAnsi((IntPtr)(void*)outFilenameFront[5]);
			 imgOriginS->ImageLocation = Marshal::PtrToStringAnsi((IntPtr)(void*)outFilenameSide[5]);
			 
			 //Xuat so do
			 char* chMeasurement = new char[1000];
			 char* chTemp = new char[100];
			 char* chTemp0 = new char[100];
			 Feature f1,f2,fG;
			 int nHeightInSide = 0;
			 if( FindFeaturePoint(ipSide,SideGotChan,fG))
				sprintf(chMeasurement,"SO DO TINH THEO PIXELS: \r\n");
			 else
			 {
				 goto result;
			 }


			 strcat(chMeasurement,"1.  Chieu cao co the: \t");
			 sprintf(chTemp,"%d\r\n",fG.y-ipSide.Ymin+1);
			 strcat(chMeasurement,chTemp);
			
			 /*strcat(chMeasurement,"2. \t");
			 sprintf(chTemp,"\r\n");
			 strcat(chMeasurement,chTemp);

			 strcat(chMeasurement,"3. \t");
			 sprintf(chTemp,"\r\n");
			 strcat(chMeasurement,chTemp);*/

			 strcat(chMeasurement,"4.   Chieu cao tu mom~ cung` vai -> ban chan: \t");
			 if( FindFeaturePoint(ipFront,FrontDinhVai1,f1))
			 {
				 nHeightInSide = ConvertToSideHeight(ipFront,ipSide,f1.y);
				 sprintf(chTemp,"%d\r\n",fG.y - nHeightInSide+1);
				 strcat(chMeasurement,chTemp);
			 }
			 else
			 {
				 sprintf(chTemp,"Khong tim thay. \r\n");
				 strcat(chMeasurement,chTemp);
			 }
			
			 strcat(chMeasurement,"5.   Chieu cao tu nach-> ban chan: \t");
			 if( FindFeaturePoint(ipFront,FrontNach1,f1))
			 {
				 nHeightInSide = ConvertToSideHeight(ipFront,ipSide,f1.y);
				 sprintf(chTemp,"%d\r\n",fG.y - nHeightInSide+1);
				 strcat(chMeasurement,chTemp);
			 }
			 else
			 {
				 sprintf(chTemp,"Khong tim thay. \r\n");
				 strcat(chMeasurement,chTemp);
			 }

			 strcat(chMeasurement,"6.   Chieu cao tu hom~ uc' co^->ban chan: \t");

			 if( FindFeaturePoint(ipSide,SideHomCo,f1))
			 {
				 sprintf(chTemp,"%d\r\n",fG.y - f1.y+1);
				 strcat(chMeasurement,chTemp);
			 }
			 else
			 {
				 sprintf(chTemp,"Khong tim thay. \r\n");
				 strcat(chMeasurement,chTemp);
			 }
			 

			
			 strcat(chMeasurement,"7.   Chieu cao tu dinh nguc -> ban chan: \t");
			 if(FindFeaturePoint(ipSide,SideDinhNguc1,f1))
			 {
				 sprintf(chTemp,"%d\r\n",fG.y - f1.y+1);
				 strcat(chMeasurement,chTemp);
			 }
			 else
			 {
				 sprintf(chTemp,"Khong tim thay\r\n");
				 strcat(chMeasurement,chTemp);
			 }

			 strcat(chMeasurement,"8.   Chieu cao han toi got chan: \t");
			 if(FindFeaturePoint(ipFront,FrontDay,f1))
			 {
				 nHeightInSide = ConvertToSideHeight(ipFront,ipSide,f1.y);
				 sprintf(chTemp,"%d\r\n",fG.y - nHeightInSide+1);
				 strcat(chMeasurement,chTemp);
			 }
			 else
			 {
				 sprintf(chTemp,"Khong tim thay.\r\n");
				 strcat(chMeasurement,chTemp);
			 }

			 strcat(chMeasurement,"9.   Chieu cao goi^ -> got chan: \t");
			 if(FindFeaturePoint(ipSide,SideDauGoi1,f1))
			 {
				 sprintf(chTemp,"%d\r\n",fG.y - f1.y+1);
				 strcat(chMeasurement,chTemp);
			 }
			 else
			 {
				sprintf(chTemp,"Khong tim thay.\r\n");
				strcat(chMeasurement,chTemp);
			 }
			 

			 strcat(chMeasurement,"19. Chieu rong vai: \t");
			 if(FindFeaturePoint(ipFront,FrontDinhVai1,f1)&&FindFeaturePoint(ipFront,FrontDinhVai2,f2))
			 {
				 sprintf(chTemp,"%d\r\n",f1.x - f2.x + 1);
				 strcat(chMeasurement,chTemp);
			 }
			 else
			 {
				sprintf(chTemp,"Khong tim thay.\r\n");
				strcat(chMeasurement,chTemp);
			 }
			 
			

			 strcat(chMeasurement,"20. Chieu rong vai con: \t");
			 if(FindFeaturePoint(ipFront,FrontDinhVai1,f1)&&FindFeaturePoint(ipFront,FrontCoVai1,f2))
			 {
				 sprintf(chTemp,"%d\r\n",f1.x - f2.x + 1);
				 strcat(chMeasurement,chTemp);
			 }
			 else
			 {
				sprintf(chTemp,"Khong tim thay.\r\n");
				strcat(chMeasurement,chTemp);
			 }
			 

			 strcat(chMeasurement,"23. Chieu dai tay: \t");
			 if(FindFeaturePoint(ipFront,FrontDinhVai1,f1)&&FindFeaturePoint(ipFront,FrontCoTay1,f2))
			 {
				 float d = sqrt(float((f1.x-f2.x)*(f1.x-f2.x)+(f1.y-f2.y)*(f1.y-f2.y)));
				 sprintf(chTemp,"%f\r\n",d);
				 strcat(chMeasurement,chTemp);
			 }
			 else
			 {
				sprintf(chTemp,"Khong tim thay.\r\n");
				strcat(chMeasurement,chTemp);
			 }


			 strcat(chMeasurement,"25. Chieu rong than qua bo*` mao chau: \t");
			 if(FindFeaturePoint(ipFront,FrontXuongHong1,f1)&&FindFeaturePoint(ipFront,FrontXuongHong2,f2))
			 {
				 sprintf(chTemp,"%d\r\n",f1.x - f2.x + 1);
				 strcat(chMeasurement,chTemp);
			 }
			 else
			 {
				 sprintf(chTemp,"Khong tim thay.\r\n");
				 strcat(chMeasurement,chTemp);
			 }


			 strcat(chMeasurement,"27. Rong ban chan: \t");
			 if(FindFeaturePoint(ipFront,FrontRongChan1,f1)&&FindFeaturePoint(ipFront,FrontRongChan2,f2))
			 {
				 sprintf(chTemp,"%d\r\n",f1.x - f2.x + 1);
				 strcat(chMeasurement,chTemp);
			 }
			 else
			 {
				sprintf(chTemp,"Khong tim thay.\r\n");
				strcat(chMeasurement,chTemp);
			 }

			 strcat(chMeasurement,"28. Dai ban chan: \t");
			 if(FindFeaturePoint(ipSide,SideBanChan1,f1)&&FindFeaturePoint(ipSide,SideBanChan2,f2))
			 {
				 sprintf(chTemp,"%d\r\n",f1.x - f2.x + 1);
				 strcat(chMeasurement,chTemp);
			 }
			 else
			 {
				sprintf(chTemp,"Khong tim thay.\r\n");
				strcat(chMeasurement,chTemp);
			 }
			
			 

			 strcat(chMeasurement,"30. Vong dau^`: \t");
			 if(FindFeaturePoint(ipFront,FrontTran1,f1)&&FindFeaturePoint(ipFront,FrontTran2,f2))
			 {
				sprintf(chTemp0,"(%d,",f1.x - f2.x + 1);
			 }
			 else
			 {
				sprintf(chTemp0,"Khong tim thay,");
			 }
			 strcat(chMeasurement,chTemp0);
			 if(FindFeaturePoint(ipSide,SideTran1,f1)&&FindFeaturePoint(ipSide,SideTran2,f2))
			 {
				sprintf(chTemp,"%d)\r\n",f1.x - f2.x + 1);
			 }
			 else
			 {
				sprintf(chTemp,"Khong tim thay) \r\n");
			 }
			 strcat(chMeasurement,chTemp);


			 strcat(chMeasurement,"34. Vong eo: \t");
			 if(FindFeaturePoint(ipFront,FrontEo1,f1)&&FindFeaturePoint(ipFront,FrontEo2,f2))
			 {
				sprintf(chTemp0,"(%d,",f1.x - f2.x + 1);
			 }
			 else
			 {
				sprintf(chTemp0,"Khong tim thay,");
			 }
			 strcat(chMeasurement,chTemp0);
			 if(FindFeaturePoint(ipSide,SideEo1,f1)&&FindFeaturePoint(ipSide,SideEo2,f2))
			 {
				sprintf(chTemp,"%d)\r\n",f1.x - f2.x + 1);
			 }
			 else
			 {
				 sprintf(chTemp,"Khong tim thay) \r\n");
			 }
			 strcat(chMeasurement,chTemp);

			 strcat(chMeasurement,"36. Vong mong: \t");
			 if(FindFeaturePoint(ipFront,FrontMong1,f1)&&FindFeaturePoint(ipFront,FrontMong2,f2))
			 {
				sprintf(chTemp0,"(%d,",f1.x - f2.x + 1);
			 }
			 else
			 {
				sprintf(chTemp0,"Khong tim thay,");
			 }
			 strcat(chMeasurement,chTemp0);
			 if(FindFeaturePoint(ipSide,SideMong1,f1)&&FindFeaturePoint(ipSide,SideMong2,f2))
			 {
				sprintf(chTemp,"%d)\r\n",f1.x - f2.x + 1);
			 }
			 else
			 {
				 sprintf(chTemp,"Khong tim thay) \r\n");
			 }
			 strcat(chMeasurement,chTemp);


			 strcat(chMeasurement,"39. Vong goi^': \t");
			 if(FindFeaturePoint(ipFront,FrontDauGoi1,f1)&&FindFeaturePoint(ipFront,FrontDauGoi2,f2))
			 {
				sprintf(chTemp0,"(%d,",f1.x - f2.x + 1);
			 }
			 else
			 {
				sprintf(chTemp0,"Khong tim thay,");
			 }
			 strcat(chMeasurement,chTemp0);
			 if(FindFeaturePoint(ipSide,SideDauGoi1,f1)&&FindFeaturePoint(ipSide,SideDauGoi2,f2))
			 {
				sprintf(chTemp,"%d)\r\n",f1.x - f2.x + 1);
			 }
			 else
			 {
				sprintf(chTemp,"Khong tim thay) \r\n");
			 }
			 strcat(chMeasurement,chTemp);


			 strcat(chMeasurement,"40. Vong bap chan: \t");
			 if(FindFeaturePoint(ipFront,FrontBapChan1,f1)&&FindFeaturePoint(ipFront,FrontBapChan2,f2))
			 {
				sprintf(chTemp0,"(%d,",f1.x - f2.x + 1);
			 }
			 else
			 {
				sprintf(chTemp0,"Khong tim thay,");
			 }
			 strcat(chMeasurement,chTemp0);
			 if(FindFeaturePoint(ipSide,SideBapChan1,f1)&&FindFeaturePoint(ipSide,SideBapChan2,f2))
			 {
				sprintf(chTemp,"%d)\r\n",f1.x - f2.x + 1);
			 }
			 else
			 {
				sprintf(chTemp,"Khong tim thay) \r\n");
			 }
			 strcat(chMeasurement,chTemp);
			 

			 strcat(chMeasurement,"43. Vong co^ chan: \t");
			 if(FindFeaturePoint(ipFront,FrontCoChan1,f1)&&FindFeaturePoint(ipFront,FrontCoChan2,f2))
			 {
				sprintf(chTemp0,"(%d,",f1.x - f2.x + 1);
			 }
			 else
			 {
				sprintf(chTemp0,"Khong tim thay,");
			 }
			 strcat(chMeasurement,chTemp0);
			 if(FindFeaturePoint(ipSide,SideCoChan1,f1)&& FindFeaturePoint(ipSide,SideCoChan2,f2))
			 {
				sprintf(chTemp,"%d)\r\n",f1.x - f2.x + 1);
			 }
			 else
			 {
				 sprintf(chTemp,"Khong tim thay)\r\n");
			 }
			 strcat(chMeasurement,chTemp);

			 txtSodo->Text =Marshal::PtrToStringAnsi((IntPtr)(void*)chMeasurement);

			 result:
			 //ghi ra file
			 string str = (string)inFilenameSide;
			 string temp;
			 int pos;
			 pos=str.find_last_of(".");
			 temp = str.substr(0,pos);
			 strcpy(outfileSodo,temp.c_str());
			 strcat(outfileSodo,"_Sodo.txt");
			 sprintf(chTemp,"File so do: %s",outfileSodo);
			 lblFileSodo->Text =Marshal::PtrToStringAnsi((IntPtr)(void*)chTemp);
			 ofstream outSodo(outfileSodo);
			 outSodo << chMeasurement;
			 outSodo.close();

			 rbtKetqua->Checked = true;
			 chTemp = NULL;
			 chTemp0 = NULL;
			 chMeasurement = NULL;

			 delete [] chTemp;
			 delete [] chTemp0;
			 delete [] chMeasurement;
			 
			 

			 Cursor = System::Windows::Forms::Cursors::Default ;
		 }
private: System::Void Form1_Leave(System::Object^  sender, System::EventArgs^  e) {

			for(int i=0;i<6;i++)
			 {
				 outFilenameFront[i]= NULL;
				 delete [] outFilenameFront[i];
				 outFilenameSide[i]= NULL;
				 delete [] outFilenameSide[i];
			 }
			outFilenameFront= NULL;
			outFilenameSide =NULL;
			 delete [] outFilenameFront;
			 delete [] outFilenameSide;
		 }
private: System::Void rbtKetqua_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			 if(outFilenameFront==NULL || outFilenameSide==NULL)
				 return;
			 imgOriginF->Load(Marshal::PtrToStringAnsi((IntPtr)(void*)outFilenameFront[5]));
			 imgOriginS->Load(Marshal::PtrToStringAnsi((IntPtr)(void*)outFilenameSide[5]));
		 }

private: System::Void rtbAnhGoc_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			
			if(!ipSide.LoadImage(inFilenameFront))
				return;
			imgOriginF->Load(Marshal::PtrToStringAnsi((IntPtr)(void*)inFilenameFront));

			if(!ipSide.LoadImage(inFilenameSide))
				return;
			imgOriginS->Load(Marshal::PtrToStringAnsi((IntPtr)(void*)inFilenameSide));
				 
		 }
private: System::Void rbtBong_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			 if(outFilenameFront==NULL || outFilenameSide==NULL)
				 return;
			 imgOriginF->Load(Marshal::PtrToStringAnsi((IntPtr)(void*)outFilenameFront[1]));
			 imgOriginS->Load(Marshal::PtrToStringAnsi((IntPtr)(void*)outFilenameSide[1]));
		 }
private: System::Void rbtDuongBien_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			 if(outFilenameFront==NULL || outFilenameSide==NULL)
				 return;
			 imgOriginF->Load(Marshal::PtrToStringAnsi((IntPtr)(void*)outFilenameFront[2]));
			 imgOriginS->Load(Marshal::PtrToStringAnsi((IntPtr)(void*)outFilenameSide[2]));
		 }
private: System::Void rbtBienMaHoa_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			 if(outFilenameFront==NULL || outFilenameSide==NULL)
				 return;
			 imgOriginF->Load(Marshal::PtrToStringAnsi((IntPtr)(void*)outFilenameFront[3]));
			 imgOriginS->Load(Marshal::PtrToStringAnsi((IntPtr)(void*)outFilenameSide[3]));
		 }
private: System::Void rbtDiemMoc_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			 if(outFilenameFront==NULL || outFilenameSide==NULL)
				 return;
			 imgOriginF->Load(Marshal::PtrToStringAnsi((IntPtr)(void*)outFilenameFront[4]));
			 imgOriginS->Load(Marshal::PtrToStringAnsi((IntPtr)(void*)outFilenameSide[4]));
		 }
};
}

